/**
 * 测试setitimer函数；
 * @author: sundx
 * @date: 2020-05-23 06:45:30
 * @Last Modified by: sundx
 * @Last Modified time: 2020-05-23 23:05:31
 */
#include <iostream>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>
void dealSig(int);
int main() {
  signal(SIGALRM, dealSig);
  struct itimerval newTimer, oldTimer;
  newTimer.it_interval.tv_sec = 2; // 周期定时秒数
  newTimer.it_interval.tv_usec = 0;
  newTimer.it_value.tv_sec = 5; //第一次定时秒数
  newTimer.it_value.tv_usec = 0;
  setitimer(ITIMER_REAL, &newTimer, &oldTimer); // oldTimer是传出
  while (1) {
    sleep(1);
    std::cout << 1 << "s" << std::endl;
  }
}
void dealSig(int sig) { std::cout << "got the signal " << sig << std::endl; }